﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using Utility;
using Utility.Service;

namespace Site
{
    public partial class ProductDetail : UserControl
    {
        private int _id;
        private int _sort;
        public int ID
        {
            set { this._id = value; }
        }

        public ProductDetail()
        {
            InitializeComponent();
            myMenu.Logo = "1";
            this.Loaded += new RoutedEventHandler(ProductDetail_Loaded);
        }

        void ProductDetail_Loaded(object sender, RoutedEventArgs e)
        {
            //菜单下滑动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = -83;
            animation.To = 0;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, myMenu);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.TopProperty));
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(animation);
            storyboard.Begin();

            InitContent(this._id);

        }

        #region InitContent
        private void InitContent(int id)
        {
            ServiceClient client = CUtility.GetServiceClient();
            client.ReadProductCompleted += new EventHandler<ReadProductCompletedEventArgs>(client_ReadProductCompleted);
            client.ReadProductAsync(id);
        }

        void client_ReadProductCompleted(object sender, ReadProductCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    this.htmlHost.SourceHtml = e.Result.Content;
                    InitTopNav(e.Result.ID);
                }
            }
            else
            {
                throw e.Error;
            }
        }
        #endregion

        #region InitTopNav
        private void InitTopNav(int id)
        {
            this.spTopNav.Children.Clear();
            ServiceClient clClass = CUtility.GetServiceClient();
            clClass.GetProductPathCompleted += new EventHandler<GetProductPathCompletedEventArgs>(clClass_GetProductPathCompleted);
            clClass.GetProductPathAsync(id);
        }

        void clClass_GetProductPathCompleted(object sender, GetProductPathCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (e.Result.Count > 0)
                {
                    HyperlinkButton lnkbtnSort = new HyperlinkButton();
                    lnkbtnSort.Tag = e.Result[0][0];
                    lnkbtnSort.Content = e.Result[0][1];
                    lnkbtnSort.Style = Application.Current.Resources["styleNavLink"] as Style;
                    lnkbtnSort.Click += new RoutedEventHandler(lnkbtnSort_Click);
                    this.spTopNav.Children.Add(lnkbtnSort);
                    TextBlock tb2 = new TextBlock();
                    tb2.Text = ">";
                    tb2.FontSize = 14;
                    this.spTopNav.Children.Add(tb2);
                    this._sort = Convert.ToInt32(e.Result[0][0].ToString());

                    for (int i = 1; i < e.Result.Count - 1; i++)
                    {
                        HyperlinkButton lnkbtnClass = new HyperlinkButton();
                        lnkbtnClass.Tag = e.Result[i][0];
                        lnkbtnClass.Content = e.Result[i][1];
                        lnkbtnClass.Style = Application.Current.Resources["styleNavLink"] as Style;
                        lnkbtnClass.Click += new RoutedEventHandler(lnkbtnClass_Click);
                        this.spTopNav.Children.Add(lnkbtnClass);

                        TextBlock tb = new TextBlock();
                        tb.Text = ">";
                        this.spTopNav.Children.Add(tb);
                    }
                    TextBlock tb1 = new TextBlock();
                    tb1.Text = e.Result[e.Result.Count - 1][1];
                    tb1.FontSize = 14;
                    this.spTopNav.Children.Add(tb1);
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void lnkbtnClass_Click(object sender, RoutedEventArgs e)
        {
            App ap = (App)Application.Current;
            ProductList pl = new ProductList();
            pl.Sort = this._sort;
            pl.ClassID = Convert.ToInt32((sender as HyperlinkButton).Tag);
            ap.RedirectTo(pl);
        }

        void lnkbtnSort_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            ProductList pl = new ProductList();
            pl.Sort = Convert.ToInt32((sender as HyperlinkButton).Tag);
            app.RedirectTo(pl);
        }

        private void lnkHome_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            app.RedirectTo(new Home());
        }

        private void lnkProduct_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            app.RedirectTo(new Product());
        }
        #endregion

        private void allProduct_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            HtmlWindow html = HtmlPage.Window;
            html.Navigate(new Uri("productlist.aspx", UriKind.Relative), "_blank");
        }
    }
}
